//
//  ViewController.m
//  sort
//
//  Created by zc on 13-11-21.
//  Copyright (c) 2013年 zc. All rights reserved.
//

#import "ViewController.h"
#import "ChineseString.h"
#import "pinyin.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic)NSMutableArray *datasorceArray;
@property (strong,nonatomic)NSMutableArray *indexArray;
@property (strong,nonatomic)NSMutableDictionary *dict;
@end

@implementation ViewController



-(NSArray *)getFirstPinYin:(NSMutableArray *)newArray{
    NSMutableArray *chineseStringsArray=[NSMutableArray array];
    
    for(int i=0;i<[newArray count];i++){
        ChineseString *chineseString=[[ChineseString alloc]init];
        chineseString.local = [newArray objectAtIndex:i];
        chineseString.string=[NSString stringWithString:[newArray objectAtIndex:i]];
        
        if(chineseString.string==nil){
            chineseString.string=@"";
        }
        
        if(![chineseString.string isEqualToString:@""]){
            NSString *pinYinResult=[NSString string];
            NSString *singlePinyinLetter=[[NSString stringWithFormat:@"%c",pinyinFirstLetter([chineseString.string characterAtIndex:0])]uppercaseString];
            
            
            pinYinResult=[pinYinResult stringByAppendingString:singlePinyinLetter];
            
            chineseString.pinYin=pinYinResult;
        }else{
            chineseString.pinYin=@"";
        }
        [chineseStringsArray addObject:chineseString];
    }
    
   
    
    return chineseStringsArray;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    _datasorceArray = [NSMutableArray array];
    [_datasorceArray addObjectsFromArray:[NSArray arrayWithObjects:@"周星",@"王小二",@"王大二",@"CC",@"hello",@"xxx",@"www",@"GGGG"@"张三",@"张四",@"李四",@"赵东",@"赵灵儿",@"仙剑奇侠传",@"混世魔王", nil]];
    _indexArray = [NSMutableArray array];
    NSArray *array =[self getFirstPinYin:_datasorceArray];
    
    
    
    
    for (ChineseString *chinsesstring in array) {

        [_indexArray addObject:chinsesstring.pinYin];
        
        
    }

    NSMutableArray *Newarray =[NSMutableArray array];
    for (int  i = 0; i<[_indexArray count]; i++) {
        if ([Newarray containsObject:_indexArray[i]]==NO) {
            [Newarray addObject:_indexArray[i]];
        }
    }
    _indexArray = Newarray;
    _dict = [[NSMutableDictionary alloc]init];
    

    for (NSString *name1 in _datasorceArray) {
        NSString *name = [self getFirstName:name1];
        if (![_dict objectForKey:name]) {
            NSMutableArray *array = [NSMutableArray array];
            [array addObject:name1];
            [_dict setObject:array forKey:name];
        }
        else{
            [[_dict objectForKey:name]addObject:name1];
        }
    
    
    
    }
    _indexArray= [_dict allKeys];
    _indexArray = [_indexArray sortedArrayUsingSelector:@selector(compare:)];

    
}

-(NSString *)getFirstName:(NSString *)str{
    return  [[NSString stringWithFormat:@"%c",pinyinFirstLetter([str characterAtIndex:0])]uppercaseString];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    NSString *str = [_indexArray objectAtIndex:section];
    NSArray *array = [_dict objectForKey:str];
    return [array count];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [_indexArray count];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [_indexArray objectAtIndex:section];
}

-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return _indexArray ;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *iden = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        
    }
    NSString *name = [_indexArray objectAtIndex:indexPath.section];
    NSArray *array = [_dict objectForKey:name];
    NSString *str = array[indexPath.row];
    
    
    
    cell.textLabel.text = str;
    return cell;
}

@end
